package com.app.controller;

import cn.hutool.core.util.StrUtil;
import com.app.common.Result;
import com.app.model.EntrepreneursUserModel;
import com.app.model.UserModel;
import com.app.service.EntrepreneursUserService;
import com.app.service.UserService;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.EntrepreneursUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 创业用户
 *
 * @author 16693103420
 * @since 2024-02-02
 */
@RestController
@RequestMapping("/entrepreneursUser")
public class EntrepreneursUserController {
    @Autowired
    private EntrepreneursUserService entrepreneursUserService;
    @Autowired
    private UserService userService;

    // 查询列表
    @GetMapping("/getEntrepreneursUserList")
    public Result getEntrepreneursUserList(@RequestParam(defaultValue = "0") Integer page,
                                           @RequestParam(defaultValue = "10") Integer limit, Integer id,String keyword) {
        EntrepreneursUserVo entrepreneursUserVo = entrepreneursUserService.getEntrepreneursUserList(page, limit, id,keyword);
        return Result.success(entrepreneursUserVo);
    }

    //用户报名
    @PostMapping("/userAddEntrepreneursUser")
    public Result userAddEntrepreneursUser(@RequestBody EntrepreneursUserModel entrepreneursUserModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        if(StrUtil.isBlankIfStr(entrepreneursUserModel.getUserName()))
        {
            return Result.error(201, "用户姓名不可为空");
        }
        if(StrUtil.isBlankIfStr(entrepreneursUserModel.getPhone()))
        {
            return Result.error(201, "手机号不可为空");
        }
        Integer userId = userModel.getId();
        entrepreneursUserModel.setUserId(userId);
        entrepreneursUserModel.setCreateTime(CommonHelp.getNowTimestamp());
        entrepreneursUserService.save(entrepreneursUserModel);
        return Result.success("报名成功");
    }

    @GetMapping("/deleteById")
    public Result deleteById(Integer id) {
        return Result.success(entrepreneursUserService.removeById(id));
    }

}
